﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataShow
{
    public partial class Online : System.Web.UI.Page
    {
        private string TableName = "TQZXFore";
        protected string IndexUrl = "/Index.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Convert.ToBoolean(Session["isadmin"]) == false)
            {
                IndexUrl = "ForecasterIndex.aspx";
            }
            string TQZX_Editor = ConfigurationManager.AppSettings["tqzxfore"].ToString();
            string[] editors = TQZX_Editor.Split(',');
            bool is_editor = false;
            if (editors.Contains(Session["nowuser"]) || Convert.ToBoolean(Session["isadmin"])==true)
            {
                is_editor = true;
            }

            btn_save.Visible = is_editor;
            btn_sync.Visible = is_editor;
            if (!IsPostBack)
            {
                txt_add_time.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        #region 保存功能
        /// <summary>
        /// 保存功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_add_time.Text))
            {
                Common.RegisterScriptAtPageLast(Page, "<script>alert('日期不能为空')</script>");
                return;
            }

            string send_point = ddl_send_point.SelectedValue;
            DateTime time = DateTime.Parse(txt_add_time.Text.Trim());

            string sql = string.Format("delete from {0} where [date]='{1}' and sendpoint='{2}'", TableName, time.ToString("yyyyMMdd"), send_point);
            new codeDAL.SqlHelper().ExecuteNonQuery(sql, null);
            List<PanelItem> items = new List<PanelItem>();
            PanelItem lyg = und_lyg.GetPanelValue();
            PanelItem gy = und_gy.GetPanelValue();
            PanelItem dh = und_dh.GetPanelValue();
            PanelItem guanyun = und_guanyun.GetPanelValue();
            PanelItem gn = und_gn.GetPanelValue();
            items.Add(lyg);
            items.Add(gy);
            items.Add(dh);
            items.Add(guanyun);
            items.Add(gn);

            List<ForeSpanType> types = new List<ForeSpanType>() { ForeSpanType.FD00_24, ForeSpanType.FD24_48, ForeSpanType.FD48_72, ForeSpanType.FD72_96, ForeSpanType.FD96_120, ForeSpanType.FD120_144, ForeSpanType.FD144_168 };
            List<SendPoint> sendpoints = new List<SendPoint>() { SendPoint.H06, SendPoint.H10, SendPoint.H16 };
            List<Element> elements = new List<Element>() { Element.maxt, Element.mint, Element.wth };
            Dictionary<string, List<string>> allData;
            for (int i = 0; i < types.Count; i++)
            {
                var nowspan = types[i];
                var nowSpan = nowspan.ToString();
                var nowSendPoint = ddl_send_point.SelectedValue;
                allData = new Dictionary<string, List<string>>();
                foreach (var item in items)
                {
                    List<string> values = item.Values.Where(x => x.Key == nowSpan).FirstOrDefault().Value;
                    allData.Add(item.StationCode, values);
                }
                new codeBLL.ForecasterBLL().AddItemSpanItem(time, nowSpan, GetRelativeClock(nowSendPoint), nowSendPoint, TableName, allData);
            }
            Common.RegisterScriptAtPageLast(Page, "alert('录入完毕')");
        } 
        #endregion

        #region 根据发报时刻获得起报时刻
        /// <summary>
        /// 根据发报时刻获得起报时刻
        /// </summary>
        /// <returns></returns>
        private string GetRelativeClock(string sendpoint)
        {
            switch (sendpoint)
            {
                case "H06": return "H08";
                case "H10": return "H08";
                case "H16": return "H20";
                default: return "ERROR";
            }
        }
        #endregion


        /// <summary>
        /// 加载本时刻预报
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Unnamed_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_add_time.Text))
            {
                Common.RegisterScriptAtPageLast(Page, "alert('日期不能为空')");
                return;
            }
            LoadData(TableName, DateTime.Parse(txt_add_time.Text.Trim()), ddl_send_point.SelectedValue);
        }



        #region 加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="table">表名</param>
        private void LoadData(string table, DateTime dt, string sendPoint)
        {
            string sql1 = string.Format("select * from {0} where date='{1}' and sendpoint='{2}'", table, dt.ToString("yyyyMMdd"), sendPoint);
            DataTable dtable = new codeDAL.SqlHelper().ExecuteQuery(sql1, CommandType.Text);
            if (dtable.Rows.Count <= 0)
            {
                Common.RegisterScriptAtPageLast(Page, "alert('暂无数据')");
                return;
            }


            Dictionary<ForeSpanType, string> searchTypes = new Dictionary<ForeSpanType, string>();
            searchTypes.Add(ForeSpanType.FD00_12, "wth");
            searchTypes.Add(ForeSpanType.FD12_24, "wth");
            searchTypes.Add(ForeSpanType.FD00_24, "maxt");
            searchTypes.Add(ForeSpanType.FD00_24_, "mint");

            searchTypes.Add(ForeSpanType.FD24_36, "wth");
            searchTypes.Add(ForeSpanType.FD36_48, "wth");
            searchTypes.Add(ForeSpanType.FD24_48, "maxt");
            searchTypes.Add(ForeSpanType.FD24_48_, "mint");

            searchTypes.Add(ForeSpanType.FD48_60, "wth");
            searchTypes.Add(ForeSpanType.FD60_72, "wth");
            searchTypes.Add(ForeSpanType.FD48_72, "maxt");
            searchTypes.Add(ForeSpanType.FD48_72_, "mint");

            searchTypes.Add(ForeSpanType.FD72_84, "wth");
            searchTypes.Add(ForeSpanType.FD84_96, "wth");
            searchTypes.Add(ForeSpanType.FD72_96, "maxt");
            searchTypes.Add(ForeSpanType.FD72_96_, "mint");

            searchTypes.Add(ForeSpanType.FD96_108, "wth");
            searchTypes.Add(ForeSpanType.FD108_120, "wth");
            searchTypes.Add(ForeSpanType.FD96_120, "maxt");
            searchTypes.Add(ForeSpanType.FD96_120_, "mint");

            searchTypes.Add(ForeSpanType.FD120_132, "wth");
            searchTypes.Add(ForeSpanType.FD132_144, "wth");
            searchTypes.Add(ForeSpanType.FD120_144, "maxt");
            searchTypes.Add(ForeSpanType.FD120_144_, "mint");

            searchTypes.Add(ForeSpanType.FD144_156, "wth");
            searchTypes.Add(ForeSpanType.FD156_168, "wth");
            searchTypes.Add(ForeSpanType.FD144_168, "maxt");
            searchTypes.Add(ForeSpanType.FD144_168_, "mint");

            PanelItem item = new PanelItem("");
            string value = "";
            codeDAL.SqlHelper sqlhelper = new codeDAL.SqlHelper();
            string exesql;
            string sql = "select value from " + table + " where [date]='" + dt.ToString("yyyyMMdd") + "' and sendpoint='" + sendPoint + "' and span='{0}' and [type]='{1}'";
            foreach (KeyValuePair<ForeSpanType, string> st in searchTypes)
            {
                string key = st.Key.ToString();
                if (key.EndsWith("_"))
                {
                    key = key.Substring(0, key.Length - 1);
                }
                exesql = string.Format(sql, key, st.Value);
                value = sqlhelper.ExecuteScalar(exesql).ToString();
                if ((int)st.Key < 4)
                {
                    item.Values["FD00_24"].Add(value); continue;
                }
                else if ((int)st.Key < 8)
                {
                    item.Values["FD24_48"].Add(value); continue;
                }
                else if ((int)st.Key < 12)
                {
                    item.Values["FD48_72"].Add(value); continue;
                }
                else if ((int)st.Key < 16)
                {
                    item.Values["FD72_96"].Add(value); continue;
                }
                else if( (int)st.Key<20 )
                {
                    item.Values["FD96_120"].Add(value); continue;
                }
                else if ((int)st.Key < 24)
                {
                    item.Values["FD120_144"].Add(value); continue;
                }
                else
                {
                    item.Values["FD144_168"].Add(value); continue;
                }
            }
            und_dh.SetValue(item);
            und_lyg.SetValue(item);
            und_gn.SetValue(item);
            und_gy.SetValue(item);
            und_guanyun.SetValue(item);
        }
        #endregion

        protected void btn_sync_Click(object sender, EventArgs e)
        {
            var station = station_code.Value; //当前要同步的station
            Dictionary<string, ascx.AddPanel2> stations = new Dictionary<string, ascx.AddPanel2>();
            stations.Add("lyg", und_lyg);
            stations.Add("gy", und_gy);
            stations.Add("gn", und_gn);
            stations.Add("guanyun", und_guanyun);
            stations.Add("dh", und_dh);
            //得到模板的值
            var value = stations[station];
            foreach (KeyValuePair<string, ascx.AddPanel2> item in stations)
            {
                if (item.Key != station)
                {
                    item.Value.SetValue(value.GetPanelValue());
                }
            }
        }
    }
}